<?php

namespace App\Helpers;

use App\Http\Models\RoleUser\RoleUser;
use App\Http\Models\RolePermissions\RolePermissions;
use App\Http\Models\Permissions\Permissions;

trait RBAC {

    //查询当前用户有哪些权限
    public function getUserPermissions($id) {
        $roleUserDatas = RoleUser::with('getRole')->where('role_user.userid', $id)->get();
        $roleId = [];
        foreach ($roleUserDatas as $roleUserData) {
            $roleId[] = $roleUserData->roleid;
        }
        $rolePermisDatas = RolePermissions::whereIn('role_permissions.roleid', $roleId)->get();
        $permissionids = [];
        foreach ($rolePermisDatas as $rolePermisData) {
            $permissionids[] = $rolePermisData->permissionsid;
        }
        $permissions = Permissions::whereIn('id', $permissionids)->get();
        $permissioName = [];
        foreach ($permissions as $permission) {
            $permissioName[] = $permission->urls;
        }
        return $permissioName;
    }

}
